import { DtoValidation } from '@/modules/core/decorators/dto-validation.decorator';
import { Injectable } from '@nestjs/common';
import { Transform } from 'class-transformer';

import {
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
  MaxLength,
  Min,
} from 'class-validator';

@Injectable()
@DtoValidation({ type: 'body', groups: ['create'] })
export class CreateGoodDto {
  @IsString()
  @MaxLength(256, {
    message: '物品名称不能超过256个字符',
  })
  @IsNotEmpty({ message: '物品名称不能为空' })
  name: string;

  @IsString()
  @IsNotEmpty({ message: '描述不能为空' })
  description: string;

  @Transform(({ value }) => Number(value))
  @IsNumber()
  @IsNotEmpty({ message: '物品价格' })
  @Min(0, { message: '物品价格不能小于0' })
  price: number;

  @IsString()
  @IsNotEmpty({ message: '物品图片不能为空', groups: ['create'] })
  @IsOptional({ groups: ['update'] })
  media: string;

  @IsString()
  @IsNotEmpty({ message: 'icon不能为空', groups: ['create'] })
  @IsOptional({ groups: ['update'] })
  mediaIcon: string;

  @IsNumber()
  @IsOptional({ always: true })
  @Transform(({ value }) => Number(value))
  @Min(0, { message: '物品价格不能小于0' })
  oldPrice?: number;

  @IsOptional()
  keywords?: string[];

  @IsNotEmpty({ message: '分类不能为空' })
  categories!: string[];

  @MaxLength(255, {
    always: true,
    message: 'deal link length cannot exceed $constraint1',
  })
  @IsOptional({ always: true })
  link?: string;
}
